package com.atguigu.tingshu.album.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackOrderVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;
	@Operation(summary = "内部接口：根据专辑id查询专辑下的所有声音id列表")
	@GuiguLogin
	@GetMapping("findTrackIdsByAlbumId/{albumId}")
	public Result<List<Long>> findTrackIdsByAlbumId(@PathVariable("albumId")Long albumId){
		return Result.ok(trackInfoService.list(Wrappers.lambdaQuery(TrackInfo.class)
				.eq(TrackInfo::getAlbumId , albumId))
				.stream().map(TrackInfo::getId).collect(Collectors.toList()));
	}
	@Operation(summary = "内部接口：根据声音id查询专辑id")
	@GuiguLogin
	@GetMapping("findAlbumIdByTrackId/{id}")
	public Result<Long> findAlbumIdByTrackId(@PathVariable("id")Long id){
		return Result.ok(trackInfoService.getById(id)
				.getAlbumId());
	}
	@Operation(summary = "内部接口：根据声音id查询它和后面N个声音详情")
	@GetMapping("findTrackInfoList/{id}/{count}")
	public Result<List<TrackInfo>>findTrackInfoList(@PathVariable("id")Long id ,
													@PathVariable("count")Integer count ){
		TrackInfo trackInfo = trackInfoService.getById(id);
		if(count==0){
			//只购买本集
			return Result.ok(Arrays.asList(trackInfo));
		}
		//select * from track_info where album_id = 1 and order_num>=1 order by order_bum limit 10;
		List<TrackInfo> trackInfos = trackInfoService.list(Wrappers.lambdaQuery(TrackInfo.class)
				.eq(TrackInfo::getAlbumId, trackInfo.getAlbumId())
				.ge(TrackInfo::getOrderNum, trackInfo.getOrderNum())
				.orderByAsc(TrackInfo::getOrderNum)
//						.notIn(TrackInfo::getId , xxx)
				.last(" limit " + count)
		);
		// 如果需要排除已购买的声音:
		return Result.ok(trackInfos);
	}
	///api/album/trackInfo/findUserTrackPaidList/48242
	@Operation(summary = "查询用户可以购买专辑的不同方案")
	@GetMapping("findUserTrackPaidList/{id}")
	@GuiguLogin
	public Result findUserTrackPaidList(@PathVariable("id")Long id){
		/*
			[
				{"name":"本集" , "price": 0.3 , "trackCount": 0 },
				{"name":"后5集" , "price": 0.3*5 , "trackCount": 5 },
				{"name":"后所有集" , "price": 0.3*所有集数 , "trackCount": 集数 },
				//如果专辑是vip免费
				{"name":"全集" ,   "price":"VIP免费","trackCount": 0 }
			]
		 */
		//TrackOrderVo: 可以封装响应数据
		List<TrackOrderVo> list = trackInfoService.findUserTrackPaidList(id);
		return Result.ok(list);
	}

//	/api/album/trackInfo/findAlbumTrackPage/761/1/10
	@GuiguLogin(required = false) //表示不需要登录
	@Operation(summary = "根据专辑id查询声音分页列表数据")
	@GetMapping("findAlbumTrackPage/{albumId}/{pageNum}/{pageSize}")
	public Result findAlbumTrackPage(
			@PathVariable("albumId") Long albumId,
			@PathVariable("pageNum") Long pageNum,
			@PathVariable("pageSize") Long pageSize
	){
		//前端需要 得到声音的分页列表，  声音使用AlbumTrackListVo封装
		Page<AlbumTrackListVo> page =  trackInfoService
				.findAlbumTrackPage(albumId,pageNum,pageSize);
		return Result.ok(page);
	}




//	/api/album/trackInfo/updateTrackInfo/51942
	@GuiguLogin
	@Operation(summary = "更新专辑")
	@PutMapping("updateTrackInfo/{id}")
	public Result  updateTrackInfo(@PathVariable Long id,
								   @RequestBody TrackInfo trackInfo){
		trackInfoService.updateTrackInfo(id,trackInfo);
		return Result.ok();
	}

	///api/album/trackInfo/getTrackInfo/51942
	@GuiguLogin
	@Operation(summary = "查询要更新的专辑")
	@GetMapping("getTrackInfo/{id}")
	public Result<TrackInfo>  getTrackInfo(@PathVariable Long id){
		TrackInfo trackInfo = trackInfoService.getTrackInfo(id);
		return Result.ok(trackInfo);
	}


	///api/album/trackInfo/removeTrackInfo/51943
	@GuiguLogin
	@Operation(summary = "删除声音")
	@DeleteMapping("removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable("id")Long id){
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

//	/api/album/trackInfo/findUserTrackPage/1/10
	@Operation(summary = "查询用户的声音分页列表")
	@GuiguLogin
	@PostMapping("findUserTrackPage/{pageNum}/{pageSize}")
	public Result findUserTrackPage(
			@PathVariable("pageNum")Long pageNum,
			@PathVariable("pageSize")Long pageSize,
			@RequestBody(required = false) TrackInfoQuery trackInfoQuery
			){
		Page<TrackListVo> trackListVoPage=  trackInfoService
				.findUserTrackPage(pageNum,pageSize , trackInfoQuery);
		return Result.ok(trackListVoPage);
	}

//	/api/album/trackInfo/saveTrackInfo
	/*
		{
		"mediaFileId":"1397757908329116060",
		"mediaUrl":"https://1259584050.vod-qcloud.com/cc67a110vodsh1259584050/f0e74a691397757908329116060/4VJbga1RM5oA.mp3",
		"trackTitle":"1-javase-数据类型",
		"coverUrl":"http://192.168.123.100:9000/tingshu/2025/04-03/1907620219296804864.png",
		"albumId":1594,
		"trackIntro":"haoting................",
		"isOpen":1
		}
	 */
	@Operation(summary = "保存声音")
	@GuiguLogin
	@PostMapping("saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfo trackInfo){
		trackInfoService.saveTrackInfo(trackInfo);
		return Result.ok();
	}



	///api/album/trackInfo/uploadTrack
	@Operation(summary = "上传声音文件")
//	@GuiguLogin
	@PostMapping("uploadTrack")
	public Result uploadTrack(MultipartFile file){
		/*
			上传声音的业务：
				如果直接使用minio存储： 权限不方便细粒度控制
				一般使用第三方的音视频点播功能保证文件安全
					- 可以将音视频上传到云点播平台保存(云点播平台可以对音视频进行 审核、还可以进行转码处理)
					- 播放时，云点播平台支持加密获取音视频播放链接 只能使用他们自己的播放器播放。播放链接还有时效性
					- 缺点： 收费
			写一个新的业务：  正常开发根据接口文档开发
				1、需要保证后端可以正确的接收前端的请求
					检查前端访问的资源路径 和后端资源路径进行对比
				2、需要正确接收前端提交的参数：
				3、后端需要响应的结果：
						按照前端的要求封装
						mediaFileId：
						mediaUrl:

		 */
		JSONObject jsonObject = vodService.uploadTrack(file);
		return Result.ok(jsonObject);
	}

}

